var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
$(function() {
	$("#probeParamList").jqGrid({
		url: IP + "GetProbeInfoList?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '选择',
			name: 'ischeck',
			edittype: "checkbox",
			width: 50,
			editable: true,
			formatter: 'checkbox',
			align: "center",
		}, {
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			key: true,
			width: 40,
			hidden: true
		}, {
			label: '设备名称',
			name: 'deviceName',
			width: 100
		}, {
			label: '设备编码',
			name: 'deviceid',
			width: 80
		}, {
			label: '地区',
			name: 'prefecture',
			width: 80,

		}, {
			label: '县市',
			name: 'city',
			width: 80,
		}, {
			label: '客户名称',
			name: 'customerName',
			width: 80,
			hidden: true,
		}, {
			label: '客户电话',
			name: 'customerTel',
			width: 100,
			hidden: true,
		}, {
			label: '客户经理',
			name: 'customerManager',
			width: 80,
			hidden: true,
		}, {
			label: '客户经理电话',
			name: 'customerManagerTel',
			width: 80,
			hidden: true,
		}, {
			label: '运营商',
			name: 'operator',
			width: 80,
			hidden: true,
		}, {
			label: '网络',
			name: 'netType',
			width: 80,
			hidden: true,
		}, {
			label: 'IP地址',
			name: 'ip',
			width: 80,
			hidden: true,
		}, {
			label: '安装地址',
			name: 'install',
			width: 100,
		}, {
			label: '设备类型',
			name: 'type',
			width: 80,
			hidden: true,
		}, {
			label: '设备状态',
			name: 'deviceStatus',
			width: 80,
			hidden: true,
		}, {
			label: 'AC',
			name: 'ac',
			width: 80,
			hidden: true,
		}, {
			label: '备注',
			name: 'memo',
			width: 150,
			hidden: true,
		}, {
			label: 'wifi名称',
			name: 'wifiName',
			width: 80,
			hidden: true,
		}, {
			label: 'wifi密码',
			name: 'wifiPwd',
			width: 80,
			hidden: true,
		}, {
			label: 'Port',
			name: 'port',
			width: 80,
			hidden: true,
		}, {
			label: '小区',
			name: 'community',
			width: 80,
			hidden: true,
		}, {
			label: 'ONU',
			name: 'onu',
			width: 80,
			hidden: true,
		}, {
			label: 'OLT',
			name: 'olt',
			width: 80,
			hidden: true,
		}, {
			label: 'Bias',
			name: 'bias',
			width: 80,
			hidden: true,
		}, {
			label: '最后一次心跳',
			name: 'lastHeartbeat',
			formatter: '',
			width: 180,
			hidden: true,
		}, {
			label: '经度',
			name: 'longitude',
			width: 80,
			hidden: true,
		}, {
			label: '纬度',
			name: 'latitude',
			width: 80,
			hidden: true,
		}, {
			label: '新增时间',
			name: 'newTime',
			width: 80,
			hidden: true,
		}, {
			label: '更新时间',
			name: 'updateTime',
			width: 80,
			hidden: true,
		}],
		//multiselect: true,
		loadonce: true,
		viewrecords: true,
		autowidth: true,
		//rownumbers:true,
		rowNum: 15,
		page: 1,
		loadtext: '正在加载数据',
		pager: "#probeParamPager",
		loadComplete: function() {
			setEditRow();
		}
	});
	var template1 = {
		"groupOp": "AND",
		"rules": [{
			"field": "deviceName",
			"op": "eq",
			"data": "www"
		}, {
			"field": "city",
			"op": "eq",
			"data": "杭州"
		}]
	};
	// activate the build in search with multiple option
	$('#probeParamList').navGrid("#probeParamPager", {
			search: true, // show search button on the toolbar
			add: false,
			edit: false,
			del: false,
			refresh: true
		}, {}, // edit options
		{}, // add options
		{}, // delete options
		{
			multipleSearch: true,
			multipleGroup: true,
			showQuery: true,
			// set the names of the template
			tmplNames: ["模板一"],
			// set the template contents
			tmplFilters: [template1]
		} // search options - define multiple search
	);

	/*****************getporbeList_function****************/
	$("#probeGroupList").jqGrid({
		url: IP + "GetProbeGroupInfo?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			align: "center",
			key: true,
			width: 50,
			hidden: true,
		}, {
			label: '探针组名称',
			name: 'probeGroup',
			width: 70,
			align: "center",
		}, {
			label: '创建人',
			name: 'cPreson',
			width: 70,
			align: "center",
		}, {
			label: '创建时间',
			name: 'cTime',
			width: 90
		}, {
			label: '备注',
			name: 'meno',
			width: 70
		}],
		loadonce: true,
		viewrecords: true,
		rowNum: 15,
		autowidth: true,
		rownumbers: true,
		page: 1,
		loadtext: '正在加载数据',
		pager: "#porbeGroupPager",
		onSelectRow: function(rowid) {
			$("#probeGroup").textbox("disable");
			var IDs = $("#probeParamList").jqGrid('getDataIDs');
			for (var i = 0; i < IDs.length; i++) {
				$("#probeParamList").jqGrid('setCell', IDs[i], 0, 'no');
			}
			$.ajax({
				url: IP + "GetProbeGroupIDS?&session=" + session + "&pid=" + rowid,
				type: "Get",
				dataType: "jsonp",
				jsonp: "callback",
				jsonpCallback: "resultHandler",
				success: function(json) {
					for (var i = 0; i < json.length; i++) {
						$("#probeParamList").jqGrid('setCell', json[i].deviceid, 0, 'Yes');
					}
					$("#probeParamList").trigger("reloadGrid")
						//setEditRow();
				}
			})
		},
	});

	var h = $("body").height();
	$("#probeGroupList").jqGrid("setGridHeight", h - 180)
	$("#probeParamList").jqGrid("setGridHeight", h - 180);
	var template2 = {
		"groupOp": "AND",
		"rules": [{
			"field": "paramGroup",
			"op": "eq",
			"data": "W"
		}, {
			"field": "cTime",
			"op": "le",
			"data": "15"
		}]
	};
	// activate the build in search with multiple option
	$('#probeGroupList').navGrid("#probeGroupPager", {
			search: true, // show search button on the toolbar
			add: false,
			edit: false,
			del: false,
			refresh: true
		}, {}, // edit options
		{}, // add options
		{}, // delete options
		{
			multipleSearch: true,
			multipleGroup: true,
			showQuery: true,
			// set the names of the template
			tmplNames: ["模板二"],
			// set the template contents
			tmplFilters: [template2]
		} // search options - define multiple search
	);
	AddRow();
})

function setEditRow() {
	var grid = $("#probeParamList");
	var ids = grid.jqGrid('getDataIDs');
	for (var i = 0; i < ids.length; i++) {
		//grid.jqGrid('setCell',ids[i],0,true);
		grid.jqGrid('editRow', ids[i]);
	}
}

function EditRow() {
	$("#probeGroup").textbox("enable");
	var rowKey = $("#probeGroupList").jqGrid('getGridParam', "selrow");
	if (rowKey) {
		$('#groupForm').form('load', {
			id: rowKey,
			probeGroup: $("#probeGroupList").getCell(rowKey, "probeGroup"),
			cPreson: $("#probeGroupList").getCell(rowKey, "cPreson"),
			meno: $("#probeGroupList").getCell(rowKey, "meno"),
		});

	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function SaveRow() {
	var cTime = getCTime();
	var grid = $("#probeGroupList");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#probeGroupList").getCell(rowKey, "probeGroup");
	var paramId = '';
	var ids = $("#probeParamList").jqGrid('getDataIDs');
	for (var i = 0; i < ids.length; i++) {
		if ($("#probeParamList").getCell(ids[i], "ischeck") == 'Yes') {
			paramId += ids[i] + ",";
		}
	}
	if (paramId == "") {
		$.messager.alert("提醒", "你还没有选测试参数", 'info');
		return;
	}
	var savestr = paramId.substr(0, paramId.length - 1);

	if ($('#groupForm').form('enableValidation').form('validate')) {
		$.ajax({
			type: "post", // 与可以 "get"
			url: IP + "UpdateProbeGroupInfo?callback=?",
			data: {
				session: session,
				savestr: savestr,
				session: session,
				id: $('#id').val(),
				probeGroup: $("#probeGroup").val(),
				cPreson: $("#cPreson").val(),
				meno: $("#meno").val(),
			},
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "resultHandler",
			success: function(json) {
				if (json.result = 200) {
					if ($('#id').val() == 0) {
						$("#probeGroupList").addRowData(json.id, {
							id: json.id,
							probeGroup: $("#probeGroup").val(),
							cPreson: $("#cPreson").val(),
							cTime: cTime,
							meno: $("#meno").val(),
						}, '');
						insertopertlog('任务管理', '探针组管理', '新增', '', $("#probeGroup").val());
						$.messager.show({
							title: '提醒',
							msg: '探针组增加成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					} else {
						$("#probeGroupList").setRowData($('#id').val(), {
							id: $('#id').val(),
							probeGroup: $("#probeGroup").val(),
							cPreson: $("#cPreson").val(),
							meno: $("#meno").val(),
						}, '');
						insertopertlog('任务管理', '探针组管理', '新增', oldvalue, $("#probeGroup").val());
						$.messager.show({
							title: '提醒',
							msg: '探针组修改成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					}
				} else {
					$.messager.alert('保存失败', json.message, 'error');
				}
				//保存成功后，重置控件，以免用户重复提交
				AddRow();
			}
		});
	}
}

function getCTime() {
	var curr_time = new Date();
	var strDate = curr_time.getFullYear() + "-";
	strDate += curr_time.getMonth() + 1 + "-";
	strDate += curr_time.getDate()+ " ";
	strDate += curr_time.getHours() + ":";
	strDate += curr_time.getMinutes() + ":";
	strDate += curr_time.getSeconds();
	return strDate;
}

function DeleteRow() {
	var grid = $("#probeGroupList");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#probeGroupList").getCell(rowKey, "probeGroup");
	if (rowKey) {
		$.messager.confirm('删除提醒', '你确认要删除' + $("#probeGroupList").getCell(rowKey, "probeGroup") + '组?', function(r) {
			if (r) {
				$.ajax({
					type: "post", // 与可以 "get"
					url: IP + "DeleteProbeGroup?&callback=? ",
					data: {
						id: rowKey,
						session: session,
					},
					dataType: "jsonp",
					jsonp: "callback",
					jsonpCallback: "resultHandler",
					success: function(json) {
						if (json.result == 200) {
							$("#probeGroupList").delRowData(rowKey);
							insertopertlog('任务管理', '探针组管理', '删除', oldvalue, '');
							$.messager.show({
								title: '提醒',
								msg: '删除成功',
								timeout: 1300,
								showType: 'slide',
								style: {
									right: '',
									bottom: ''
								}
							});
						} else {
							$.messager.alert('删除失败', json.message, 'error');
						}
					}
				});
			}
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
	AddRow();
}

function AddRow() {
	$("#probeGroup").textbox("enable");
	$('#groupForm').form('load', {
		id: 0,
		probeGroup: '',
		cPreson: sessionStorage.getItem("realName"),
		meno: '',
	})
	$("#probeGroupList").trigger("reloadGrid");
	var IDs = $("#probeParamList").jqGrid('getDataIDs');
	for (var i = 0; i < IDs.length; i++) {
		$("#probeParamList").jqGrid('setCell', IDs[i], 0, 'no');
	}
	$("#probeParamList").trigger("reloadGrid");
}




